/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainFrame.java
 *
 * Created on 28 févr. 2011, 16:31:14
 */

package gui;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.event.TableModelEvent;
import javax.swing.tree.TreePath;
import model.CIMBadArgument;
import model.IPTableRule;
import model.IPSecRule;
import model.IPSecPolicy;
import java.awt.CardLayout;
import java.awt.Component;
import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTree;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TableModelListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.Position.Bias;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.MutableTreeNode;
import javax.swing.tree.TreeNode;
import model.CIMServer;

/**
 *
 * @author Dudu
 */
public class MainFrame extends javax.swing.JFrame {
    private ArrayList<IPSecRule> IPSecRules;
    private ArrayList<IPTableRule> IPTableRules;
    private ArrayList<IPSecPolicy> IPSecPolicies;
    private CIMServer server;
    private IPSecPolicyTable ipSecPolicyTable;
    private IPSecRuleTable ipSecRuleTable;
    private IPTableRuleTable ipTableRuleTable;
    private GlobalTablePanel globalPanel;
    /** Creates new form MainFrame */
    public MainFrame() {
        IPSecRules=new ArrayList<IPSecRule>();
        IPSecPolicies=new ArrayList<IPSecPolicy>();
        IPTableRules=new ArrayList<IPTableRule>();
        try {
            server = new CIMServer(null, null, null, null);
        }
        catch (Exception ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
        globalPanel=new GlobalTablePanel(IPSecPolicies,IPSecRules,IPTableRules);
        ipSecPolicyTable=new IPSecPolicyTable(IPSecPolicies,this);
        ipSecRuleTable=new IPSecRuleTable(IPSecRules,this);
        ipTableRuleTable=new IPTableRuleTable(IPTableRules,this);
        /**Sample rules and policies to view the result*/
        /*IPSecRule rule1=new IPSecRule("ipsec rule 1","127.0.0.1","dest 1","type 1","transport type 1","spi 1","algo 1","key 1");
        IPSecRule rule2=new IPSecRule("ipsec rule 2","source 2","dest 2","type 2","transport type 2","spi 2","algo 2","key 2");
        IPSecRule rule3=new IPSecRule("ipsec rule 3","127.0.0.1","dest 3","type 3","transport type 3","spi 3","algo 3","key 3");
        IPSecRule rule4=new IPSecRule("ipsec rule 4","source 4","dest 4","type 4","transport type 4","spi 4","algo 4","key 4");
        IPSecRule rule5=new IPSecRule("ipsec rule 5","127.0.0.1","dest 5","type 5","transport type 5","spi 5","algo 5","key 5");




        IPSecPolicy policy1=new IPSecPolicy("ipsec policy 1","source 1","dest 1","protocol 1","direction 1","action 1","tunnel_in 1","tunnel_out 1","Type 1","Transport_type 1","Level 1");
        IPSecPolicy policy2=new IPSecPolicy("ipsec policy 2","127.0.0.1","dest 2","protocol 2","direction 2","action 2","tunnel_in 1","tunnel_out 1","Type 1","Transport_type 1","Level 1");
        IPSecPolicy policy3=new IPSecPolicy("ipsec policy 3","source 3","dest 3","protocol 3","direction 3","action 3","tunnel_in 1","tunnel_out 1","Type 1","Transport_type 1","Level 1");
        IPSecPolicy policy4=new IPSecPolicy("ipsec policy 4","source 4","127.0.0.1","protocol 4","direction 4","action 4","tunnel_in 1","tunnel_out 1","Type 1","Transport_type 1","Level 1");
        IPSecPolicy policy5=new IPSecPolicy("ipsec policy 5","127.0.0.1","dest 5","protocol 5","direction 5","action 5","tunnel_in 1","tunnel_out 1","Type 1","Transport_type 1","Level 1");
        IPSecPolicy policy6=new IPSecPolicy("ipsec policy 6","source 6","dest 6","protocol 6","direction 6","action 6","tunnel_in 1","tunnel_out 1","Type 1","Transport_type 1","Level 1");

        IPTableRule iptablerule1=new IPTableRule("iptable rule 1","chain 1","source 1","dest 1","protocol 1","source port 1","dest port 1","jump 1");
        IPTableRule iptablerule2=new IPTableRule("iptable rule 2","chain 2","127.0.0.1","dest 2","protocol 2","source port 2","dest port 2","jump 2");
        IPTableRule iptablerule3=new IPTableRule("iptable rule 3","chain 3","source 3","dest 3","protocol 3","source port 3","dest port 3","jump 3");
        IPTableRule iptablerule4=new IPTableRule("iptable rule 4","chain 4","source 4","dest 4","protocol 4","source port 4","dest port 4","jump 4");
        IPTableRule iptablerule5=new IPTableRule("iptable rule 5","chain 5","source 5","dest 5","protocol 5","source port 5","dest port 5","jump 5");
        IPTableRule iptablerule6=new IPTableRule("iptable rule 6","chain 6","source 6","dest 6","protocol 6","source port 6","dest port 6","jump 6");
        IPTableRule iptablerule7=new IPTableRule("iptable rule 7","chain 7","source 7","dest 7","protocol 7","source port 7","dest port 7","jump 7");
*/
System.out.println("sample data created");



        initComponents();
        jTree1.addTreeSelectionListener((TreeSelectionListener) new DefaultTreeSelectionListener(jTree1,this));
        CardLayout cards=(CardLayout) jPanel3.getLayout();
        ipSecPolicyTable.getJTable1().getSelectionModel().addListSelectionListener(new SelectionListener(ipSecPolicyTable.getJTable1(),this));
        ipSecRuleTable.getJTable1().getSelectionModel().addListSelectionListener(new SelectionListener(ipSecRuleTable.getJTable1(),this));
        ipTableRuleTable.getJTable1().getSelectionModel().addListSelectionListener(new SelectionListener(ipTableRuleTable.getJTable1(),this));

        globalPanel.getjTable1().getSelectionModel().addListSelectionListener(new SelectionListener( globalPanel.getjTable1(),this));
        globalPanel.getjTable2().getSelectionModel().addListSelectionListener(new SelectionListener(globalPanel.getjTable2(),this));
        globalPanel.getjTable3().getSelectionModel().addListSelectionListener(new SelectionListener(globalPanel.getjTable3(),this));


        jPanel3.add("IPTables and IPSec config",globalPanel);
        jPanel3.add("IPSec policies",ipSecPolicyTable);
        jPanel3.add("IPSec rules",ipSecRuleTable);
        jPanel3.add("IPTables rules",ipTableRuleTable);
        cards.show(jPanel3,"global panel");
        /*this.addIPSecPolicy(policy1);
        this.addIPSecPolicy(policy2);
        this.addIPSecPolicy(policy3);
        this.addIPSecPolicy(policy4);
        this.addIPSecPolicy(policy5);
        this.addIPSecPolicy(policy6);

        this.addIPSecRule(rule1);
        this.addIPSecRule(rule2);
        this.addIPSecRule(rule3);
        this.addIPSecRule(rule4);
        this.addIPSecRule(rule5);

        this.addIPTableRule(iptablerule1);
        this.addIPTableRule(iptablerule2);
        this.addIPTableRule(iptablerule3);
        this.addIPTableRule(iptablerule4);
        this.addIPTableRule(iptablerule5);
        this.addIPTableRule(iptablerule6);
        this.addIPTableRule(iptablerule7);*/

        Toolkit tk = Toolkit.getDefaultToolkit();
        int xSize = ((int) tk.getScreenSize().getWidth());
        int ySize = ((int) tk.getScreenSize().getHeight());
        this.setSize(xSize,ySize);

           jFrame1.setLocation((xSize - jFrame1.getSize().width) / 2,
                   (ySize - jFrame1.getSize().height) / 2);
           jDialog1.setLocation((xSize - jDialog1.getSize().width) / 2,
                   (ySize - jDialog1.getSize().height) / 2);
           jDialog2.setLocation((xSize - jDialog2.getSize().width) / 2,
                   (ySize - jDialog2.getSize().height) / 2);
           jTextArea1.setEditable(false);

    }

//RefrshALL n'a pas l'effet attendu au niveau du JTree... Je ne sais pas pk. Je laisse de coté pour le mom

    public void refreshAll(){
        globalPanel=new GlobalTablePanel(IPSecPolicies,IPSecRules,IPTableRules);
        ipSecPolicyTable=new IPSecPolicyTable(IPSecPolicies,this);
        ipSecRuleTable=new IPSecRuleTable(IPSecRules,this);
        ipTableRuleTable=new IPTableRuleTable(IPTableRules,this);
        this.initComponents();
        jTree1.addTreeSelectionListener((TreeSelectionListener) new DefaultTreeSelectionListener(jTree1,this));
        CardLayout cards=(CardLayout) jPanel3.getLayout();
        ipSecPolicyTable.getJTable1().getSelectionModel().addListSelectionListener(new SelectionListener(ipSecPolicyTable.getJTable1(),this));
        jPanel3.add("IPTables and IPSec config",globalPanel);
        jPanel3.add("IPSec policies",ipSecPolicyTable);
        jPanel3.add("IPSec rules",ipSecRuleTable);
        jPanel3.add("IPTables rules",ipTableRuleTable);
        cards.show(jPanel3,"global panel");



        Iterator itr1=((ArrayList<IPSecPolicy>)IPSecPolicies.clone()).iterator();
                    int i=0;
        while(itr1.hasNext()){
            i++;
            IPSecPolicy policy=(IPSecPolicy) itr1.next();
            JTable table=ipSecPolicyTable.getJTable1();
        JTable globalTable=globalPanel.getjTable3();
        DefaultTreeModel model=(DefaultTreeModel) jTree1.getModel();
        DefaultMutableTreeNode root=(DefaultMutableTreeNode) model.getRoot();
        MutableTreeNode node=(MutableTreeNode) root.getChildAt(2);
        model.insertNodeInto(new javax.swing.tree.DefaultMutableTreeNode(policy.getName()),node,model.getChildCount(node));
        DefaultTableModel tableModel=(DefaultTableModel) table.getModel();
        tableModel.addRow(new Object[] {policy.getName(),policy.getSource(),policy.getDestination(),policy.getProtocol(),policy.getDirection(),policy.getAction(),policy.getTunnel_in(),policy.getTunnel_out(),policy.getType(),policy.getTransport_type(),policy.getLevel()});
        IPSecPolicyForm policyForm=new IPSecPolicyForm(policy,this);
        jPanel3.add(policy.getName(),policyForm);
     DefaultTableModel globalTableModel=(DefaultTableModel) globalTable.getModel();
        globalTableModel.addRow(new Object[] {policy.getName(),policy.getSource(),policy.getDestination(),policy.getProtocol(),policy.getDirection(),policy.getAction(),policy.getTunnel_in(),policy.getTunnel_out(),policy.getType(),policy.getTransport_type(),policy.getLevel()});

        }


        Iterator itr2=IPSecRules.iterator();
        i=0;
        while(itr2.hasNext()){
            i++;
            IPSecRule rule=(IPSecRule) itr2.next();
            JTable table=ipSecRuleTable.getJTable1();
        JTable globalTable=globalPanel.getjTable1();
        DefaultTreeModel model=(DefaultTreeModel) jTree1.getModel();
        DefaultMutableTreeNode root=(DefaultMutableTreeNode) model.getRoot();
        MutableTreeNode node=(MutableTreeNode) root.getChildAt(2);
        model.insertNodeInto(new javax.swing.tree.DefaultMutableTreeNode(rule.getName()),node,model.getChildCount(node));
        DefaultTableModel tableModel=(DefaultTableModel) table.getModel();
        tableModel.addRow(new Object[] {rule.getName(),rule.getSource(),rule.getDestination(),rule.getType(),rule.getTransport_type(),rule.getSpi(),rule.getAlgorythm(),rule.getKey()});
        IPSecRuleForm policyForm=new IPSecRuleForm(rule,this);
        jPanel3.add(rule.getName(),policyForm);
     DefaultTableModel globalTableModel=(DefaultTableModel) globalTable.getModel();
        globalTableModel.addRow(new Object[] {rule.getName(),rule.getSource(),rule.getDestination(),rule.getType(),rule.getTransport_type(),rule.getSpi(),rule.getAlgorythm(),rule.getKey()});

        }


        Iterator itr3=IPTableRules.iterator();
        i=0;
        while(itr3.hasNext()){
            i++;
            IPTableRule rule=(IPTableRule) itr3.next();
            JTable table=ipTableRuleTable.getJTable1();
        JTable globalTable=globalPanel.getjTable3();
        DefaultTreeModel model=(DefaultTreeModel) jTree1.getModel();
        DefaultMutableTreeNode root=(DefaultMutableTreeNode) model.getRoot();
        MutableTreeNode node=(MutableTreeNode) root.getChildAt(2);
        model.insertNodeInto(new javax.swing.tree.DefaultMutableTreeNode(rule.getName()),node,model.getChildCount(node));
        DefaultTableModel tableModel=(DefaultTableModel) table.getModel();
        tableModel.addRow(new Object[] {rule.getName(),rule.getChain(), rule.getSource(), rule.getDestination(), rule.getProtocol(), rule.getSource_port(), rule.getDestination_port(), rule.getJump()});
        IPTableRuleForm policyForm=new IPTableRuleForm(rule,this);
        jPanel3.add(rule.getName(),policyForm);
     DefaultTableModel globalTableModel=(DefaultTableModel) globalTable.getModel();
        globalTableModel.addRow(new Object[] {rule.getName(),rule.getChain(), rule.getSource(), rule.getDestination(), rule.getProtocol(), rule.getSource_port(), rule.getDestination_port(), rule.getJump()});

        }
    }

    /*public void refreshTables(){
        JPanel globalPanel=(JPanel) new GlobalTablePanel(IPSecPolicies,IPSecRules,IPTableRules);
        JPanel ipSecPolicyTable=(JPanel) new IPSecPolicyTable(IPSecPolicies);
        JPanel ipSecRuleTable=(JPanel) new IPSecRuleTable(IPSecRules);
        JPanel ipTableRuleTable=(JPanel) new IPTableRuleTable(IPTableRules);
    }*/

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jFrame1 = new javax.swing.JFrame();
        jLabel2 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jPasswordField1 = new javax.swing.JPasswordField();
        jDialog1 = new javax.swing.JDialog();
        jLabel3 = new javax.swing.JLabel();
        jButton4 = new javax.swing.JButton();
        jDialog2 = new javax.swing.JDialog();
        jPanel4 = new javax.swing.JPanel();
        jButton6 = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTree1 = new javax.swing.JTree();
        jPanel3 = new javax.swing.JPanel();

        jFrame1.setTitle("Change server");
        jFrame1.setAlwaysOnTop(true);
        jFrame1.setMinimumSize(new java.awt.Dimension(400, 220));
        jFrame1.setResizable(false);

        jLabel2.setText("Host");

        jTextField1.setText(server.getHost());
        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });

        jButton2.setText("Cancel");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("OK");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jLabel4.setText("User login");

        jLabel5.setText("Password");

        jTextField2.setText(server.getUser());

        jPasswordField1.setText(server.getPwd());

        javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane());
        jFrame1.getContentPane().setLayout(jFrame1Layout);
        jFrame1Layout.setHorizontalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jFrame1Layout.createSequentialGroup()
                .addGap(38, 38, 38)
                .addGroup(jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton2)
                    .addComponent(jLabel2)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPasswordField1)
                    .addComponent(jTextField2)
                    .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 136, Short.MAX_VALUE)
                    .addComponent(jButton3, javax.swing.GroupLayout.Alignment.TRAILING))
                .addGap(42, 42, 42))
        );
        jFrame1Layout.setVerticalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jFrame1Layout.createSequentialGroup()
                .addGap(16, 16, 16)
                .addGroup(jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 26, Short.MAX_VALUE)
                .addGroup(jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jButton3))
                .addGap(25, 25, 25))
        );

        jDialog1.setAlwaysOnTop(true);

        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel3.setText("An error occured trying to reach server at "+server.getHost()+" with namespace "+server.getNameSpace()+" using account "+server.getUser());
        jDialog1.getContentPane().add(jLabel3, java.awt.BorderLayout.CENTER);

        jButton4.setText("OK");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });
        jDialog1.getContentPane().add(jButton4, java.awt.BorderLayout.PAGE_END);

        jDialog2.setMinimumSize(new java.awt.Dimension(463, 177));

        jButton6.setText("OK");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGap(176, 176, 176)
                .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(174, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addContainerGap(43, Short.MAX_VALUE)
                .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jDialog2.getContentPane().add(jPanel4, java.awt.BorderLayout.PAGE_END);

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane2.setViewportView(jTextArea1);

        jDialog2.getContentPane().add(jScrollPane2, java.awt.BorderLayout.CENTER);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        jLabel1.setText(setServerMessage());

        jButton1.setText("Change server");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton5.setText("Refresh instances");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(39, 39, 39)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 363, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(193, 193, 193)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 226, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton5)
                .addContainerGap(107, Short.MAX_VALUE))
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButton1, jButton5});

        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(41, 41, 41)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton5))
                .addContainerGap(22, Short.MAX_VALUE))
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jButton1, jButton5});

        javax.swing.tree.DefaultMutableTreeNode treeNode1 = new javax.swing.tree.DefaultMutableTreeNode("IPTables and IPSec config");
        javax.swing.tree.DefaultMutableTreeNode treeNode2 = new javax.swing.tree.DefaultMutableTreeNode("IPTables rules");
        treeNode1.add(treeNode2);
        treeNode2 = new javax.swing.tree.DefaultMutableTreeNode("IPSec policies");
        treeNode1.add(treeNode2);
        treeNode2 = new javax.swing.tree.DefaultMutableTreeNode("IPSec rules");
        treeNode1.add(treeNode2);
        jTree1.setModel(new javax.swing.tree.DefaultTreeModel(treeNode1));
        jTree1.setName("tree"); // NOI18N
        jScrollPane1.setViewportView(jTree1);
        jTree1.getAccessibleContext().setAccessibleName("tree");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 344, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 551, Short.MAX_VALUE)
        );

        jPanel3.setMaximumSize(new java.awt.Dimension(903, 551));
        jPanel3.setMinimumSize(new java.awt.Dimension(903, 551));
        jPanel3.setLayout(new java.awt.CardLayout());

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 810, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, 551, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
       jTextField1.setText(server.getHost());
        jFrame1.setVisible(true);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        jFrame1.setVisible(false);       
}//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed

        try {
            CIMServer new_server = new CIMServer("root/cimv2", jTextField1.getText(), jTextField2.getText(), jPasswordField1.getPassword().toString());
            server=new_server;
            jFrame1.setVisible(false);
            jLabel1.setText(setServerMessage());
        } catch (Exception ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
        jFrame1.setVisible(false);
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        jDialog1.setVisible(false);
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField1ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        refreshInstances();
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        jDialog2.setVisible(false);
    }//GEN-LAST:event_jButton6ActionPerformed


    public void setRightPanel(String selection){
        CardLayout cards=(CardLayout) jPanel3.getLayout();
        cards.show(jPanel3,selection);



    }
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MainFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JDialog jDialog1;
    private javax.swing.JDialog jDialog2;
    private javax.swing.JFrame jFrame1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPasswordField jPasswordField1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    protected javax.swing.JTree jTree1;
    // End of variables declaration//GEN-END:variables


    //Add IPSecRule to the GUI. Warning: unless you also call server.register(rule), this will not add it to the CIMOM.
    public void addIPSecRule(IPSecRule rule){

    JTable table=ipSecRuleTable.getJTable1();
    JTable globalTable=globalPanel.getjTable3();
    IPSecRules.add(rule);
    DefaultTreeModel model=(DefaultTreeModel) jTree1.getModel();
    DefaultMutableTreeNode root=(DefaultMutableTreeNode) model.getRoot();
    MutableTreeNode node=(MutableTreeNode) root.getChildAt(2);
    model.insertNodeInto(new javax.swing.tree.DefaultMutableTreeNode(rule.getName()),node,model.getChildCount(node));
    DefaultTableModel tableModel=(DefaultTableModel) table.getModel();
    tableModel.addRow(new Object[] {rule.getName(),rule.getSource(),rule.getDestination(),rule.getType(),rule.getTransport_type(),rule.getSpi(),rule.getAlgorythm(),rule.getKey()});
    IPSecRuleForm ruleForm=new IPSecRuleForm(rule,this);
    ruleForm.setName(rule.getName());
    jPanel3.add(rule.getName(),ruleForm);
    DefaultTableModel globalTableModel=(DefaultTableModel) globalTable.getModel();
    globalTableModel.addRow(new Object[] {rule.getName(),rule.getSource(),rule.getDestination(),rule.getType(),rule.getTransport_type(),rule.getSpi(),rule.getAlgorythm(),rule.getKey()});
    this.setRightPanel(rule.getName());

     }
    //Add IPSecPolicy to the GUI. Warning: unless you also call server.register(policy), this will not add it to the CIMOM.
    public void addIPSecPolicy(IPSecPolicy policy){


    JTable table=ipSecPolicyTable.getJTable1();
    JTable globalTable=globalPanel.getjTable2();
    DefaultTreeModel model=(DefaultTreeModel) jTree1.getModel();
    DefaultMutableTreeNode root=(DefaultMutableTreeNode) model.getRoot();
    MutableTreeNode node=(MutableTreeNode) root.getChildAt(1);
    model.insertNodeInto(new javax.swing.tree.DefaultMutableTreeNode(policy.getName()),node,model.getChildCount(node));
    DefaultTableModel tableModel=(DefaultTableModel) table.getModel();
    tableModel.addRow(new Object[] {policy.getName(),policy.getSource(),policy.getDestination(),policy.getProtocol(),policy.getDirection(),policy.getAction(),policy.getTunnel_in(),policy.getTunnel_out(),policy.getType(),policy.getTransport_type(),policy.getLevel()});
    IPSecPolicyForm policyForm=new IPSecPolicyForm(policy,this);
    policyForm.setName(policy.getName());
    jPanel3.add(policy.getName(),policyForm);
    DefaultTableModel globalTableModel=(DefaultTableModel) globalTable.getModel();
    globalTableModel.addRow(new Object[] {policy.getName(),policy.getSource(),policy.getDestination(),policy.getProtocol(),policy.getDirection(),policy.getAction(),policy.getTunnel_in(),policy.getTunnel_out(),policy.getType(),policy.getTransport_type(),policy.getLevel()});
    this.setRightPanel(policy.getName());

  
    }
    //Add IPTableRule to the GUI. Warning: unless you also call server.register(rule), this will not add it to the CIMOM.
    public void addIPTableRule(IPTableRule rule){

    JTable table=ipTableRuleTable.getJTable1();
    JTable globalTable=globalPanel.getjTable1();
    DefaultTreeModel model=(DefaultTreeModel) jTree1.getModel();
    DefaultMutableTreeNode root=(DefaultMutableTreeNode) model.getRoot();
    MutableTreeNode node=(MutableTreeNode) root.getChildAt(0);
    model.insertNodeInto(new javax.swing.tree.DefaultMutableTreeNode(rule.getName()),node,model.getChildCount(node));
    DefaultTableModel tableModel=(DefaultTableModel) table.getModel();
    tableModel.addRow(new Object[] {rule.getName(),rule.getChain(), rule.getSource(), rule.getDestination(), rule.getProtocol(), rule.getSource_port(), rule.getDestination_port(), rule.getJump()});
    IPTableRuleForm ruleForm=new IPTableRuleForm(rule,this);
    ruleForm.setName(rule.getName());
    jPanel3.add(rule.getName(),ruleForm);
    DefaultTableModel globalTableModel=(DefaultTableModel) globalTable.getModel();
    globalTableModel.addRow(new Object[] {rule.getName(),rule.getChain(), rule.getSource(), rule.getDestination(), rule.getProtocol(), rule.getSource_port(), rule.getDestination_port(), rule.getJump()});
    this.setRightPanel(rule.getName());

    }
    //Open an empty IPSecPolicyForm. Does not affect CIMOM.
    public void createIPSecPolicy(){
        IPSecPolicy policy=new IPSecPolicy();
        IPSecPolicyForm form=new IPSecPolicyForm(policy,this);
        jPanel3.add("New IPSecPolicy form",form);
        this.setRightPanel("New IPSecPolicy form");
    }
    //Open an empty IPSecRuleForm. Does not affect CIMOM.
    void createIPSecRule() {
        IPSecRule rule=new IPSecRule();
        IPSecRuleForm form=new IPSecRuleForm(rule,this);
        jPanel3.add("New IPSecRule form",form);
        this.setRightPanel("New IPSecRule form");
    }

    //Open an empty IPTableRuleForm. Does not affect CIMOM.
    void createIPTableRule() {
        IPTableRule rule=new IPTableRule();
        IPTableRuleForm form=new IPTableRuleForm(rule,this);
        jPanel3.add("New IPTableRule form",form);
        this.setRightPanel("New IPTableRule form");    }

    //Modify a IPSecPolicy in the GUI and the CIMOM.
    void modifyIPSecPolicy(IPSecPolicy old_policy, IPSecPolicy policy) {
        try {
            //La CIMOM
            server.remove(old_policy);
            server.register(policy);
            int i = 0;
            //l'array-list
            IPSecPolicies.remove(old_policy);
            IPSecPolicies.add(policy);
            //Le table de globalpanel
            JTable globalTable = globalPanel.getjTable2();
            DefaultTableModel globalTableModel = (DefaultTableModel) globalTable.getModel();
            while (i < 10000) {
                String name = (String) globalTable.getValueAt(i, 0);
                if (name.equals(old_policy.getName())) {
                    globalTableModel.removeRow(i);
                    globalTableModel.insertRow(i, new Object[]{policy.getName(), policy.getSource(), policy.getDestination(), policy.getProtocol(), policy.getDirection(), policy.getAction(), policy.getTunnel_in(), policy.getTunnel_out(), policy.getType(), policy.getTransport_type(), policy.getLevel()});
                    break;
                }
                i++;
            }
            //Le table IPSec policies
            i = 0;
            JTable table = ipSecPolicyTable.getJTable1();
            DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
            while (i < 10000) {
                String name = (String) table.getValueAt(i, 0);
                if (name.equals(old_policy.getName())) {
                    tableModel.removeRow(i);
                    tableModel.insertRow(i, new Object[]{policy.getName(), policy.getSource(), policy.getDestination(), policy.getProtocol(), policy.getDirection(), policy.getAction(), policy.getTunnel_in(), policy.getTunnel_out(), policy.getType(), policy.getTransport_type(), policy.getLevel()});
                    break;
                }
                i++;
            }
            //Jtree... (la galere)
            DefaultTreeModel model = (DefaultTreeModel) jTree1.getModel();
            TreePath path = jTree1.getNextMatch(old_policy.getName(), 0, Bias.Forward);
            MutableTreeNode node = (MutableTreeNode) path.getLastPathComponent();
            node.setUserObject(policy.getName());
            jTree1.setSelectionPath(jTree1.getNextMatch("IPTables and IPSec config", 0, Bias.Forward));
            //Le layout du JPanel3
            CardLayout layout = (CardLayout) jPanel3.getLayout();
            Component[] comp = jPanel3.getComponents();
            for (i = 0; i < comp.length; i++) {
                if (comp[i].getName() == null ? old_policy.getName() == null : comp[i].getName().equals(old_policy.getName())) {
                    comp[i].setName(policy.getName());
                    layout.removeLayoutComponent(comp[i]);
                    layout.addLayoutComponent(comp[i], policy.getName());
                    break;
                }
            }
            jTree1.setSelectionPath(jTree1.getNextMatch(policy.getName(), 0, Bias.Forward));
        } catch (Exception ex) {
            jDialog2.setVisible(true);
            jTextArea1.setText("A CIM error occured. See User Guide for further details.\n\nError Trace : "+ex.getMessage());
        }
    }
   //Modify a IPSecRule in the GUI and the CIMOM.
    void modifyIPSecRule(IPSecRule old_rule, IPSecRule rule) {
        try {
            //La CIMOM
            server.remove(old_rule);
            server.register(rule);
            int i = 0;
            //l'array-list
            IPSecRules.remove(old_rule);
            IPSecRules.add(rule);
            //Le table de globalpanel
            JTable globalTable = globalPanel.getjTable3();
            DefaultTableModel globalTableModel = (DefaultTableModel) globalTable.getModel();
            while (i < 10000) {
                String name = (String) globalTable.getValueAt(i, 0);
                if (name.equals(old_rule.getName())) {
                    globalTableModel.removeRow(i);
                    globalTableModel.insertRow(i, new Object[]{rule.getName(), rule.getSource(), rule.getDestination(), rule.getType(), rule.getTransport_type(), rule.getSpi(), rule.getAlgorythm(), rule.getKey()});
                    break;
                }
                i++;
            }
            //Le table IPSec policies
            i = 0;
            JTable table = ipSecRuleTable.getJTable1();
            DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
            while (i < 10000) {
                String name = (String) table.getValueAt(i, 0);
                if (name.equals(old_rule.getName())) {
                    tableModel.removeRow(i);
                    tableModel.insertRow(i, new Object[]{rule.getName(), rule.getSource(), rule.getDestination(), rule.getType(), rule.getTransport_type(), rule.getSpi(), rule.getAlgorythm(), rule.getKey()});
                    break;
                }
                i++;
            }
           //Jtree... (la galere)
            DefaultTreeModel model = (DefaultTreeModel) jTree1.getModel();
            TreePath path = jTree1.getNextMatch(old_rule.getName(), 0, Bias.Forward);
            MutableTreeNode node = (MutableTreeNode) path.getLastPathComponent();
            node.setUserObject(rule.getName());
            jTree1.setSelectionPath(jTree1.getNextMatch("IPTables and IPSec config", 0, Bias.Forward));
            //Le layout du JPanel3
            CardLayout layout = (CardLayout) jPanel3.getLayout();
            Component[] comp = jPanel3.getComponents();
            for (i = 0; i < comp.length; i++) {
                if (comp[i].getName() == null ? old_rule.getName() == null : comp[i].getName().equals(old_rule.getName())) {
                    comp[i].setName(rule.getName());
                    layout.removeLayoutComponent(comp[i]);
                    layout.addLayoutComponent(comp[i], rule.getName());
                    break;
                }
            }
            jTree1.setSelectionPath(jTree1.getNextMatch(rule.getName(), 0, Bias.Forward));
        } catch (Exception ex) {
            jDialog2.setVisible(true);
            jTextArea1.setText("A CIM error occured. See User Guide for further details.\n\nError Trace : "+ex.getMessage());
        }
    }
   //Modify a IPTableRule in the GUI and the CIMOM.
    void modifyIPTableRule(IPTableRule old_rule, IPTableRule rule) {
        try {
            //La CIMOM
            server.remove(old_rule);
            server.register(rule);
            int i = 0;
            //l'array-list
            IPTableRules.remove(old_rule);
            IPTableRules.add(rule);
            //Le table de globalpanel
            JTable globalTable = globalPanel.getjTable1();
            DefaultTableModel globalTableModel = (DefaultTableModel) globalTable.getModel();
            while (i < 10000) {
                String name = (String) globalTable.getValueAt(i, 0);
                if (name.equals(old_rule.getName())) {
                    globalTableModel.removeRow(i);
                    globalTableModel.insertRow(i,new Object[] {rule.getName(),rule.getChain(), rule.getSource(), rule.getDestination(), rule.getProtocol(), rule.getSource_port(), rule.getDestination_port(), rule.getJump()});
                    break;
                }
                i++;
            }
            //Le table IPSec policies
            i = 0;
            JTable table = ipSecRuleTable.getJTable1();
            DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
            while (i < 10000) {
                String name = (String) table.getValueAt(i, 0);
                if (name.equals(old_rule.getName())) {
                    tableModel.removeRow(i);
                    tableModel.insertRow(i,new Object[] {rule.getName(),rule.getChain(), rule.getSource(), rule.getDestination(), rule.getProtocol(), rule.getSource_port(), rule.getDestination_port(), rule.getJump()});
                    break;
                }
                i++;
            }
            //Jtree... (la galere)
            DefaultTreeModel model = (DefaultTreeModel) jTree1.getModel();
            TreePath path = jTree1.getNextMatch(old_rule.getName(), 0, Bias.Forward);
            MutableTreeNode node = (MutableTreeNode) path.getLastPathComponent();
            node.setUserObject(rule.getName());
            jTree1.setSelectionPath(jTree1.getNextMatch("IPTables and IPSec config", 0, Bias.Forward));
            //Le layout du JPanel3
            CardLayout layout = (CardLayout) jPanel3.getLayout();
            Component[] comp = jPanel3.getComponents();
            for (i = 0; i < comp.length; i++) {
                if (comp[i].getName() == null ? old_rule.getName() == null : comp[i].getName().equals(old_rule.getName())) {
                    comp[i].setName(rule.getName());
                    layout.removeLayoutComponent(comp[i]);
                    layout.addLayoutComponent(comp[i], rule.getName());
                    break;
                }
            }
            jTree1.setSelectionPath(jTree1.getNextMatch(rule.getName(), 0, Bias.Forward));
        } catch (Exception ex) {
            jDialog2.setVisible(true);
            jTextArea1.setText("A CIM error occured. See User Guide for further details.\n\nError Trace : "+ex.getMessage());
        }


    }
//Remove an IPTableRule from the GUI. Does not affect the CIMOM
    void deleteIPTableRule(IPTableRule old_rule){

        int i=0;

        //l'array-list
        IPTableRules.remove(old_rule);

        //Le table de globalpanel
        JTable globalTable=globalPanel.getjTable1();
        DefaultTableModel globalTableModel=(DefaultTableModel) globalTable.getModel();
        while(i<10000){
            String name=(String) globalTable.getValueAt(i, 0);
            if (name.equals(old_rule.getName())){
                globalTableModel.removeRow(i);
                break;
            }
            i++;
        }



        //Le table IPSec policies
        i=0;
        JTable table=ipTableRuleTable.getJTable1();
        DefaultTableModel tableModel=(DefaultTableModel) table.getModel();
        while(i<10000){
            String name=(String) table.getValueAt(i, 0);
            if (name.equals(old_rule.getName())){
                tableModel.removeRow(i);
                break;
            }
            i++;
        }

        //Jtree... (la galere)
            DefaultTreeModel model=(DefaultTreeModel) jTree1.getModel();
            TreePath path=jTree1.getNextMatch(old_rule.getName(),0, Bias.Forward);
            MutableTreeNode node=(MutableTreeNode) path.getLastPathComponent();
            model.removeNodeFromParent(node);

//Le layout du JPanel3
    CardLayout layout=(CardLayout) jPanel3.getLayout();
    Component[] comp=jPanel3.getComponents();
    for(i=0;i<comp.length;i++){
        if( comp[i].getName() == null ? old_rule.getName() == null : comp[i].getName().equals(old_rule.getName())){
            layout.removeLayoutComponent(comp[i]);
            break;
        }
    }
    jTree1.setSelectionPath(jTree1.getNextMatch("IPTable rules",0, Bias.Forward));
    }
    //Remove an IPSecRule from the GUI. Does not affect the CIMOM
    void deleteIPSecRule(IPSecRule old_rule) {

        int i=0;

        //l'array-list
        IPSecRules.remove(old_rule);

        //Le table de globalpanel
        JTable globalTable=globalPanel.getjTable3();
        DefaultTableModel globalTableModel=(DefaultTableModel) globalTable.getModel();
        while(i<10000){
            String name=(String) globalTable.getValueAt(i, 0);
            if (name.equals(old_rule.getName())){
                globalTableModel.removeRow(i);
                break;
            }
            i++;
        }



        //Le table IPSec policies
        i=0;
        JTable table=ipSecRuleTable.getJTable1();
        DefaultTableModel tableModel=(DefaultTableModel) table.getModel();
        while(i<10000){
            String name=(String) table.getValueAt(i, 0);
            if (name.equals(old_rule.getName())){
                tableModel.removeRow(i);
                break;
            }
            i++;
        }

        //Jtree... (la galere)
            DefaultTreeModel model=(DefaultTreeModel) jTree1.getModel();
            TreePath path=jTree1.getNextMatch(old_rule.getName(),0, Bias.Forward);
            MutableTreeNode node=(MutableTreeNode) path.getLastPathComponent();
            model.removeNodeFromParent(node);

//Le layout du JPanel3
    CardLayout layout=(CardLayout) jPanel3.getLayout();
    Component[] comp=jPanel3.getComponents();
    for(i=0;i<comp.length;i++){
        if( comp[i].getName() == null ? old_rule.getName() == null : comp[i].getName().equals(old_rule.getName())){
            layout.removeLayoutComponent(comp[i]);
            break;
        }
    }
    jTree1.setSelectionPath(jTree1.getNextMatch("IPSec rules",0, Bias.Forward));
    }

    //Remove an IPSecPolicy from the GUI. Does not affect the CIMOM
    void deleteIPSecPolicy(IPSecPolicy old_policy){
        int i=0;

        //l'array-list
        IPSecPolicies.remove(old_policy);

       //Le table de globalpanel
        JTable globalTable=globalPanel.getjTable2();
        DefaultTableModel globalTableModel=(DefaultTableModel) globalTable.getModel();
        while(i<10000){
            String name=(String) globalTable.getValueAt(i, 0);
            if (name.equals(old_policy.getName())){
                globalTableModel.removeRow(i);
                break;
            }
            i++;
        }
        //Le table IPSec policies
        i=0;
        JTable table=ipSecPolicyTable.getJTable1();
        DefaultTableModel tableModel=(DefaultTableModel) table.getModel();
        while(i<10000){
            String name=(String) table.getValueAt(i, 0);
            if (name.equals(old_policy.getName())){
                tableModel.removeRow(i);
                break;
            }
            i++;
        }
        //Jtree... (la galere)
            DefaultTreeModel model=(DefaultTreeModel) jTree1.getModel();
            TreePath path=jTree1.getNextMatch(old_policy.getName(),0, Bias.Forward);
            MutableTreeNode node=(MutableTreeNode) path.getLastPathComponent();
            model.removeNodeFromParent(node);
        

//Le layout du JPanel3
    CardLayout layout=(CardLayout) jPanel3.getLayout();
    Component[] comp=jPanel3.getComponents();
    for(i=0;i<comp.length;i++){
        if( comp[i].getName() == null ? old_policy.getName() == null : comp[i].getName().equals(old_policy.getName())){
            layout.removeLayoutComponent(comp[i]);
            break;
        }
    }
    jTree1.setSelectionPath(jTree1.getNextMatch("IPSec policies",0, Bias.Forward));
    }

    public String setServerMessage(){
        if(server.getHost()==null){
            return "You are not connected on a CIMServer.";
        }
 else{
            return "You are currently connected on : "+server.getHost()+" with user "+server.getUser();
 }
    }


    //Rafraichit les données de la GUI, n'altere pas la CIMOM
    private void refreshInstances() {
        try {
            ArrayList<IPSecPolicy> new_IPSecPolicies = server.enumIPSecPolicy();
            //Retrait des anciennes IPSecPolicies de la GUI
            Iterator itr = IPSecPolicies.iterator();
            while (itr.hasNext()) {
                IPSecPolicy p = (IPSecPolicy) itr.next();
                deleteIPSecPolicy(p);
            }
            //Rajout des nouvelles
            itr = new_IPSecPolicies.iterator();
            while (itr.hasNext()) {
                IPSecPolicy p = (IPSecPolicy) itr.next();
                addIPSecPolicy(p);
            }
            //Idem avec IPSecRule
            ArrayList<IPSecRule> new_IPSecRules = server.enumIPSecRule();
            itr = IPSecRules.iterator();
            while (itr.hasNext()) {
                IPSecRule r = (IPSecRule) itr.next();
                deleteIPSecRule(r);
            }
            itr = new_IPSecRules.iterator();
            while (itr.hasNext()) {
                IPSecRule r = (IPSecRule) itr.next();
                addIPSecRule(r);
            }
            //Idem avec IPSecPolicy
            ArrayList<IPTableRule> new_IPTableRules = server.enumIPTableRule();
            itr = IPTableRules.iterator();
            while (itr.hasNext()) {
                IPTableRule r = (IPTableRule) itr.next();
                deleteIPTableRule(r);
            }
            itr = new_IPTableRules.iterator();
            while (itr.hasNext()) {
                IPTableRule r = (IPTableRule) itr.next();
                addIPTableRule(r);
            }
        } catch (Exception ex) {
            jDialog2.setVisible(true);
            jTextArea1.setText("A CIM error occured. See User Guide for further details.\n\nError Trace : "+ex.getMessage());
        }
    }
//Add an IPSecPolicy to the CIMOM (does not affect the GUI)
public void register(IPSecPolicy p){
        try {
            server.register(p);
        } catch (Exception ex) {
            jDialog2.setVisible(true);
            jTextArea1.setText("A CIM error occured. See User Guide for further details.\n\nError Trace : "+ex.getMessage());
        }
}
//Add an IPSecRule to the CIMOM (does not affect the GUI)
public void register(IPSecRule r){
        try {
            server.register(r);
        } catch (Exception ex) {
            jDialog2.setVisible(true);
            jTextArea1.setText("A CIM error occured. See User Guide for further details.\n\nError Trace : "+ex.getMessage());
        }
}
//Add an IPTableRule to the CIMOM (does not affect the GUI)
public void register(IPTableRule r){
        try {
            server.register(r);
        } catch (Exception ex) {
            jDialog2.setVisible(true);
            jTextArea1.setText("A CIM error occured. See User Guide for further details.\n\nError Trace : "+ex.getMessage());
        }
}
//Remove an IPSecPolicy from the CIMOM (does not affect the GUI)
public void remove(IPSecPolicy p){
        try {
            server.remove(p);
        } catch (Exception ex) {
            jDialog2.setVisible(true);
            jTextArea1.setText("A CIM error occured. See User Guide for further details.\n\nError Trace : "+ex.getMessage());
        }
}
//Remove an IPSecRule from the CIMOM (does not affect the GUI)
public void remove(IPSecRule r){
        try {
            server.remove(r);
        } catch (Exception ex) {
            jDialog2.setVisible(true);
            jTextArea1.setText("A CIM error occured. See User Guide for further details.\n\nError Trace : "+ex.getMessage());
        }
}
//Remove an IPTableRule from the CIMOM (does not affect the GUI)
public void remove(IPTableRule r){
        try {
            server.remove(r);
        } catch (Exception ex) {
            jDialog2.setVisible(true);
            jTextArea1.setText("A CIM error occured. See User Guide for further details.\n\nError Trace : "+ex.getMessage());
        }
}


class DefaultTreeSelectionListener implements TreeSelectionListener{
    private JTree tree;
    private MainFrame frame;

    public DefaultTreeSelectionListener(JTree tree, MainFrame frame) {
        this.tree = tree;
        this.frame = frame;
    }
    public void valueChanged(TreeSelectionEvent e) {
        try{
    DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
        frame.setRightPanel((String) node.getUserObject());
        return ;
        }catch(NullPointerException excep){
            //osef
        }
}
}
class SelectionListener implements ListSelectionListener {
    private JTable table;
    private MainFrame frame;

    SelectionListener(JTable table,MainFrame frame) {
        this.table = table;
        this.frame=frame;
    }
    public void valueChanged(ListSelectionEvent e) {
        try{
        if (e.getSource() == table.getSelectionModel()){
            int row=table.getSelectedRow();
            String selection=(String) table.getValueAt(row, 0);
            frame.setRightPanel(selection);
             
    }
        } catch (NullPointerException ex) {
        //osef
        }
    }
}
    }
